<template>
  <div id="detail">
    <detail-nav-bar
      class="detail-nav"
      @titleClick="titleClick"
      ref="nav"
    ></detail-nav-bar>
    <scroll class="content" ref="scroll" @scroll="contentscroll" :probeType="3">
      <detail-swiper :top-images="topImages"></detail-swiper>
      <detail-base-info :goods="goods"></detail-base-info>
      <detail-shop-info :shop="shop"></detail-shop-info>
      <detail-goods-info
        :detail-info="detailInfo"
        @imageLoad="imageLoad"
      ></detail-goods-info>
      <detail-param-info :paramInfo="paramInfo" ref="param"></detail-param-info>
      <detail-comment ref="comment"></detail-comment>
      <detail-recommend ref="recommend"></detail-recommend>
    </scroll>
    <detail-bottom-bar @addToCart="addToCart"></detail-bottom-bar>
    <back-top @click.native="backclick" v-show="isShowBackTop"></back-top>
    <!-- <toast :message="message" :show="show"></toast> -->
  </div>
</template>
<script>
import DetailNavBar from "views/detail/childcoms/DetailNavBar.vue";
import DetailSwiper from "views/detail/childcoms/DetailSwiper.vue";
import DetailBaseInfo from "views/detail/childcoms/DetailBaseInfo.vue";
import DetailShopInfo from "views/detail/childcoms/DetailShopInfo.vue";
import DetailGoodsInfo from "views/detail/childcoms/DetailGoodsInfo.vue";
import DetailParamInfo from "views/detail/childcoms/DetailParamInfo.vue";
import DetailComment from "views/detail/childcoms/DetailComment.vue";
import DetailRecommend from "views/detail/childcoms/DetailRecommend.vue";
import DetailBottomBar from "views/detail/childcoms/DetailBottomBar.vue";

import Scroll from "components/common/scroll/Scroll";
// import Toast from "components/common/toast/toast.vue";

import { getDetail, Goods, Shop, GoodsParam } from "network/detail";
// 导入混入对象
import { backTopMixin } from "common/mixin";

import { mapActions } from "vuex";

export default {
  name: "Detail",
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    Scroll,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailComment,
    DetailRecommend,
    DetailBottomBar,
    // Toast,
  },
  data() {
    return {
      iid: 0,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      themeTopsY: [],
      currentIndex: 0,
      // message: "",
      // show: false,
    };
  },
  mixins: [backTopMixin],
  created() {
    //   1.保存传入的id
    this.iid = this.$route.params.id;
    // 2.根据id查询数据
    getDetail(this.iid).then((res) => {
      // 进行数据分离
      const data = res.result;
      //   console.log(data);
      //   1.获取顶部轮播图数据
      this.topImages = data.itemInfo.topImages;
      //   2.获取商品信息
      this.goods = new Goods(
        data.itemInfo,
        data.columns,
        data.shopInfo.services
      );
      //   3.获取店铺信息
      this.shop = new Shop(data.shopInfo);
      // 4.保存商品详情信息
      this.detailInfo = data.detailInfo;
      // 5.获取参数信息
      this.paramInfo = new GoodsParam(
        data.itemParams.info,
        data.itemParams.rule
      );

      this.$nextTick(() => {});
    });
  },
  methods: {
    // 此方法将vuex中的actions中的方法映射到methods中
    ...mapActions(["addCart"]),
    addToCart() {
      // 1.获取商品信息
      const product = {};
      // console.log(this.goods);
      product.image = this.topImages[0];
      product.title = this.goods.title;
      product.desc = this.goods.desc;
      product.price = this.goods.realPrice;
      product.iid = this.iid;

      // console.log(product);

      // 2.加入购物车信息到vuex中进行保存
      // this.$store.commit("addCart", product);

      // 处理toast
      // this.$store.dispatch("addCart", product).then((res) => {
      //   console.log(res);
      // });

      // 此方法通过mapActions引入
      this.addCart(product).then((res) => {
        // this.message = res;
        // this.show = true;
        // setTimeout(() => {
        //   this.show = false;
        //   this.message = "";
        // }, 1000);
        // console.log(res);
        this.$toast.show(res, 1000);
        // this.$toast.show();
      });
    },
    imageLoad() {
      this.$refs.scroll.refresh();
      this.getthemeTopsY();
    },
    // 导航参数获取模块
    getthemeTopsY() {
      this.themeTopsY = [];
      this.themeTopsY.push(0);
      this.themeTopsY.push(this.$refs.param.$el.offsetTop);
      this.themeTopsY.push(this.$refs.comment.$el.offsetTop);
      this.themeTopsY.push(this.$refs.recommend.$el.offsetTop);
      this.themeTopsY.push(Number.MAX_VALUE);

      // console.log(this.themeTopsY);
    },
    titleClick(index) {
      this.$refs.scroll.scrollTo(0, -this.themeTopsY[index], 200);
    },

    contentscroll(position) {
      // 判断backtop是否显示
      this.listenShowBackTop(position);
      // this.isShowBackTop = -position.y > 300;
      // tabbar与内容联动
      const positionY = -position.y;
      let length = this.themeTopsY.length;
      for (let i = 0; i < length - 1; i++) {
        if (
          this.currentIndex != i &&
          positionY >= this.themeTopsY[i] &&
          positionY < this.themeTopsY[i + 1]
        ) {
          this.currentIndex = i;
          this.$refs.nav.currentIndex = this.currentIndex;
        }
      }
    },
  },
};
</script>

<style scoped>
#detail {
  position: relative;
  z-index: 99;
  background-color: #fff;
  height: 100vh;
}

.detail-nav {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  z-index: 9;
  background-color: #fff;
}
.content {
  overflow: hidden;
  position: absolute;
  top: 44px;
  right: 0;
  left: 0;
  bottom: 0px;
  margin-bottom: 49px;
}
</style>